package com.xonro.common.histrans.sfeg;

import com.actionsoft.bpms.commons.formfile.model.FormFileModel;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.model.Drp2FileModel;
import org.apache.commons.collections.CollectionUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author:zhuw
 * @Description:
 * @Date 2022/3/14
 */
public class FileUtil {

    public static final String path="http://zzdrp.com/api/workflow/security/baseCommon/downloadFile?";

    //目前token先在网页的请求上获取，实时获取需要通过drp2.0的token获取接口
    public static final String drpToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MDAyOTE2OTMsImF1dGhvckpzb24iOiJ7XCJ0aW1lU3RhbXBcIjpcIjE3MDAyOTE2OTMxMDRcIixcImxvZ2luT3JnSWRcIjpcIjAwMjIwNFwiLFwiY29ycElkXCI6XCJBcHBEZWZhdWx0XCIsXCJzaWduYXR1cmVcIjpcIjVSWW8xRkVGeklRZG5pQ1VFTVNmRjdVZmFrYXp3eGZpSjcwUzc0Rzk3eE1xTERSQTJVRkgzcjdielNJYW1uSWhLWG5iWWVzcGgyNTklMEFhVkcyZnRuZ01iU3prb1YzJTJCdzFPXCIsXCJ0ZW5hbnRJZFwiOlwiQlBNXCIsXCJsb2dpblVzZXJJZFwiOlwiOWM1NmE3OWItOGI2MC00NzYwLThkZjQtNzU5ODhmMjMyNzZjXCIsXCJub25jZVwiOlwiMTA5NFwifSIsImlhdCI6MTcwMDIwNTI5M30.6BksRqZTtHNF8Ra5dzLLkukmDsj1oeufnSaOe3WCcvk";

    /**
    *@Author:JF
    *@Description:DRP 2.0文件解析
    *@Date:2023/11/16 13:41
    *@parm:  * @param null
    */
    public  static void uploadFileByJsonArray(FileTransData fileData) throws IOException {
        //DRP2.0系统文件json格式如下
        //[{"fileName":"永达保时捷幕墙(1).docx","groupName":"workflow","filePathName":"2023/07/26c7ef6fa9f34ec0bcea0dc23dcbb16b.docx"}]

        List<Drp2FileModel> fileList = JSONArray.parseArray(fileData.getFileJson(), Drp2FileModel.class);
        if(CollectionUtils.isEmpty(fileList)){
            return  ;
        }

        for(Drp2FileModel file:fileList){
            FormFileModel formFileModel = new FormFileModel();
            formFileModel.setBoName(fileData.getBoName());
            formFileModel.setBoId(fileData.getBoId());
            formFileModel.setFileName(file.getFileName());
            formFileModel.setBoItemName(fileData.getFieldName());
            formFileModel.setProcessInstId(fileData.getProcessInstId());
            formFileModel.setCreateUser(fileData.getUserId());//
            formFileModel.setCreateDate(fileData.getCreateDate());
            formFileModel.setAppId(fileData.getAppId());
            String paramString="fileName="+URLEncoder.encode(file.getFileName(),"utf-8")+"&groupName="+file.getGroupName()+"&filePath="+file.getFilePathName();
            String url=path+paramString;
            SDK.getLogAPI().consoleInfo("获取附件url："+url);
            HttpURLConnection conn = FileHelper.Drp2Download( url,drpToken);
            SDK.getLogAPI().consoleInfo("获取下载的附件url的连接信息："+conn);
            if(null!=conn){
                InputStream inputStream = conn.getInputStream();
               // getInputStream(inputStream,file.getFileName());
                formFileModel.setFileSize(inputStream.read());
                FileHelper.upFile(formFileModel, inputStream);
            }
        }

        return ;
    }

    private static void createFile(File file) throws IOException {
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        file.createNewFile();
    }


    public static  void getInputStream(InputStream fileInput,String fileName) throws IOException {

        File localPath=new File("D://drp/"+fileName);
        createFile(localPath);
        FileOutputStream fileOutputStream = new FileOutputStream(localPath);
       // ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 ];
        int n ;
        InputStream inputStream = null;
        try {
            while ((n = fileInput.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, n);

            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
}
